﻿//This file is part of XCOM ModManager

// XCOM ModManager is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XCMod
{
    public partial class Config : Form
    {

        string configPath;
        Form1 form1;
      
        public Config(string s, Form1 form1obj)
        {
            InitializeComponent();
            configPath = s;
            form1 = form1obj;
        }

        private void Config_Load(object sender, EventArgs e)
        {
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                tbFilePath.Text = folderBrowserDialog1.SelectedPath ;
            }
        }

        private void btnModrBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                tbModrFilePath.Text = folderBrowserDialog1.SelectedPath;
            }
        } 

        private void btnSave_Click(object sender, EventArgs e)
        {
            XmlDocument configXML = new XmlDocument();//create new XML document
            XmlNode node = configXML.CreateNode(XmlNodeType.XmlDeclaration, "", "");//create xml declaration
            configXML.AppendChild(node);//append declaration to xml doc
            //<Filepaths> and its children
            XmlElement filePaths = configXML.CreateElement("ConfigInfo");//create parent node
            //<ModAuthor>
            XmlElement modAuthorElem = configXML.CreateElement("ModAuthor");//create modauthor element
            modAuthorElem.InnerText = tbAuthorName.Text;//element data
            filePaths.AppendChild(modAuthorElem);//append element to parent node
            //<XCOM>
            XmlElement filePathElem = configXML.CreateElement("XCOM");//create filepath element
            filePathElem.InnerText = tbFilePath.Text;//element data
            filePaths.AppendChild(filePathElem);//append element to parent node
            //<Unpacked>
            XmlElement filePathUPKElem = configXML.CreateElement("Unpacked");//create upk filepath element
            filePathUPKElem.InnerText = tbModrFilePath.Text;//element data
            filePaths.AppendChild(filePathUPKElem);//append element to parent node            

            configXML.AppendChild(filePaths);//append parent node to file
            try
            {
                configXML.Save(configPath + "config.xml");//save the xml doc
            }
            catch (Exception ex)
            {
                //error handling
            }

            form1.readConfig();
            Close();
        }

        public void loadFields(string filepath, string unpacked, string author)
        {
            tbFilePath.Text = filepath;
            tbModrFilePath.Text = unpacked;
            tbAuthorName.Text = author;
        }

               
    }
}
